package com.kcnet.qk.activity;

import android.app.ActivityManager;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.FrameLayout;

import com.kcnet.qk.Common.FuncItemFactory;
import com.kcnet.qk.R;
import com.kcnet.qk.adapter.FuncAdapter;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.MyWindowManager;
import com.kcnet.qk.controller.addfriend.AddFriendAdoptCTR;
import com.kcnet.qk.controller.addfriend.AddFriendFriendCTR;
import com.kcnet.qk.controller.addfriend.AddFriendPhoneCTR;
import com.kcnet.qk.controller.addfriend.AddFriendRecommedCTR;
import com.kcnet.qk.controller.addfriend.AddSearchPhoneCTR;
import com.kcnet.qk.util.AppUtil;
import com.kcnet.qk.util.PreferenceHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import io.netty.handler.codec.dns.AbstractDnsOptPseudoRrRecord;

/**
 * Created by Administrator on 2018/4/27.
 */

public class AddFirendActivity extends BaseActivity {
    private final static int MINI_VERSION_CODE = 16;
    private final static String SNACKBAR_TXT = "当前您使用的系统版本过低，请更新高版本系统再使用";//ASD

    @BindView(R.id.ll)
    FrameLayout mLlRootView;

    @BindView(R.id.bannerContainer)
    FrameLayout mFlBannerContainer;

    @BindView(R.id.ryv)
    RecyclerView mRecyclerView;

    private boolean mIsFirstJump = false;
    private FuncAdapter mAdapter;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new FuncAdapter(this, FuncItemFactory.getInstance().createmAddFriendFuncItems());
        mRecyclerView.setAdapter(mAdapter);
        //AppUtil.jumpToWx(this);
    }

    @Override
    protected void initData() {
        // only for build version larger equal than api 18

    }

    @Override
    protected void setListener() {
        mAdapter.setOnItemClickListener(new FuncAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                String funcName = FuncItemFactory.getInstance().createmAddFriendFuncItems().get(position).funcName;
                switch (funcName) {


                    case FuncItemFactory.ADD_FIREND_PHONE:
                        AddFriendPhoneCTR.getInstance().setParms(5,2,5);
                        startAddPeople(Const.KEY_ADD_FIREND_PHONE);
                        break;
                    case FuncItemFactory.ADD_FIREND_FRIED:
                        AddFriendFriendCTR.getInstance().setParms(5,2,5);
                        startAddPeople(Const.KEY_ADD_FIREND_FRIED);
                        break;
                    case FuncItemFactory.ADD_FIREND_RECOMMED:
                        AddFriendRecommedCTR.getInstance().setParms(5,2,5);
                        startAddPeople(Const.KEY_ADD_FIREND_RECOMMED);
                        break;

                    case FuncItemFactory.ADD_FIREND_ADOPT:
                        AddFriendAdoptCTR.getInstance().setParms(5,2,5);
                        startAddPeople(Const.KEY_ADD_FIREND_ADOPT);
                        break;
//                    case FuncItemFactory.ADD_FIREND_HOME:
//
//                        startAddPeople(Const.KEY_ADD_FIREND_HOME);
//                        break;

                    case FuncItemFactory.ADD_FRIEND_SEATCH_NAME:
                        ArrayList list = new ArrayList();
                        list.add("茜茜");
                        list.add("凯凯");
                        AddSearchPhoneCTR.getInstance().setParms(5,2,5
                                ,"台南",list);

                        startAddPeople(Const.KEY_ADD_FRIEND_SEATCH_NAME);
                        break;
                    case FuncItemFactory.ADD_FRIEND_SEATCH_EMAIL:
                        ArrayList list2 = new ArrayList();
                        list2.add("茜茜");
                        list2.add("凯凯");
                        AddSearchPhoneCTR.getInstance().setParms(5,2,5
                                ,"台南",list2);
                        startAddPeople(Const.KEY_ADD_FRIEND_SEATCH_EMAIL);
                        break;

                    case FuncItemFactory.STOP_ADD_PEOPLE:
                        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, false);
                        EventBus.getDefault().post(new Boolean(true));
                        toast("设置成功");
                        break;
                    case FuncItemFactory.FLOW_WINDOW:
                        showFlowWindow();
                        break;
                }
            }
        });
    }


    private void startAddPeople(final int addType) {
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        prepareAddPeople(addType);
    }

    private void prepareAddPeople(int addType) {

        PreferenceHelper.putInt(Const.PREF_KEY_ADD_PEOPLE_TYPE, addType);
        EventBus.getDefault().post(new Boolean(true));
        EventBus.getDefault().post(new Integer(addType));
        if (!AppUtil.checkPermission(this)) {
            jumpSetting();
            mIsFirstJump = true;
        } else {

            AppUtil.jumpToFb(this);
        }
    }



    private void jumpSetting() {
        // jump to setting permission
        Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        startActivity(intent);
    }


//    @Override
//    protected void onResume() {
//        if (mIsFirstJump && AppUtil.checkPermission(this)) {
//            AppUtil.jumpToWx(this);
//            mIsFirstJump = false;
//        }
//        super.onResume();
//    }

    private void showFlowWindow() {
        if (!MyWindowManager.isWindowShowing()) {
            MyWindowManager.createSmallWindow(getApplicationContext());
        } else {
            toast("已显示悬浮窗了");
        }
    }

}
